package com.offcn.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.Dao.BookDao;
import com.offcn.bean.BookBean;
import com.offcn.bean.ResultVO;
import com.offcn.util.PageUtils;
import com.offcn.util.UpdateFileUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@MultipartConfig
@WebServlet("/book")
public class BookServlet extends BaseServlet {

    //dao层
    private BookDao bookDao = new BookDao();

    //修改图书信息
    private void updateBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//利用jar包接收学生数据

        Map<String, String[]> parameterMap = req.getParameterMap();
        BookBean book = new BookBean();
        try {
            BeanUtils.populate(book,parameterMap);
            //更换图片操作图片
            Part part = req.getPart("photo");

            String photo="";
            //判断是否是新图片上传
            if(part.getSize()>0){
               photo= UpdateFileUtil.updateFile(part);

            }else{
                photo=req.getParameter("oldPhoto");
            }
            book.setPhoto(photo);

            String[] fits = req.getParameterValues("fit");
            //遍历适合人群的书籍
            String fit="";
            for (String  str:fits){
                fit+=str+'#';
            }
            fit=fit.substring(0,fit.length()-1);
            book.setFit(fit);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用dao层
        int num=bookDao.updateBook(book);
        //给出响应
        if (num>0){
           // selectBook(req, resp);
            //转发查询页面时，会带模糊查询的参数，应使用重定向
            //resp.sendRedirect("selectBook.jsp");不带数据，不能使用
            resp.sendRedirect("book?method=selectBook");
        }

    }

    //查询全部图书
    private void selectBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<BookBean> list = bookDao.selectBook();

        //2.给出响应
        ResultVO vo=null;
        if (list!= null) {
            vo=new ResultVO(200,"查询成功",list);
        }else{
            vo=new ResultVO(500,"查询失败",null);
        }
        //转为json字符串
        ObjectMapper mapper = new ObjectMapper();
        resp.getWriter().print(mapper.writeValueAsString(vo));

    }


    //查询详细信息
    private void selectOneBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收的id
        String bid = req.getParameter("id");
        int id = Integer.parseInt(bid);
        //调用dao层
        BookBean book = bookDao.selectOneBook(id);
        //给出响应
        ResultVO vo=null;
        if (book!= null) {
            vo=new ResultVO(200,"查询成功",book);
        }else{
            vo=new ResultVO(500,"查询失败",null);
        }
        //转为json字符串
        ObjectMapper mapper = new ObjectMapper();
        resp.getWriter().print(mapper.writeValueAsString(vo));
    }



    //删除图书
    private void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        //接收id
        String id1 = req.getParameter("id");
        int id = Integer.parseInt(id1);

        //调用dao层
        int num = bookDao.deleteBook(id);
        //做出响应
        ResultVO vo=null;
        if (num!= 0) {
            vo=new ResultVO(200,"删除成功",num);
            selectBook(req, resp);
        }else{
            vo=new ResultVO(500,"删除失败",null);
        }
//        //转为json字符串
     ObjectMapper mapper = new ObjectMapper();
      resp.getWriter().print(mapper.writeValueAsString(vo));
//        if (num > 0) {
//            selectBook(req, resp);
//        } else {
//            resp.sendRedirect("fail.jsp");
//        }
    }

    //添加图书
    private void addBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //接收前端传来的数据
        String bname = req.getParameter("bname");
        Double price = Double.parseDouble(req.getParameter("price"));
        String author = req.getParameter("author");
        String home = req.getParameter("home");
        String daTime = req.getParameter("daTime");
        String discrie = req.getParameter("discrie");

        //接收书籍种类
        String kinds = req.getParameter("kinds");
        //遍历适合人群的书籍
        String fits1 = req.getParameter("fit");
        String fit = fits1.replace(",", "#");
        //接收图片
        Part part = req.getPart("photo");
        //调用上传的工具类
        String photo= UpdateFileUtil.updateFile(part);

        BookBean s = new BookBean(bname, price, author,home, daTime, discrie,kinds,fit,photo);
        // //处理请求 访问数据库 代码划分 调用Dao层
        int num = bookDao.addBook(s);
        //给出响应
        ResultVO vo=null;
        if (num!= 0) {
            vo=new ResultVO(200,"添加成功",num);
            selectBook(req, resp);
        }else{
            vo=new ResultVO(500,"添加失败",null);
        }
//        //转为json字符串
        ObjectMapper mapper = new ObjectMapper();
        resp.getWriter().print(mapper.writeValueAsString(vo));


    }



}